from owlready2 import *
import rdflib

print('1. storing the quadstore in an SQLite3 file database')
sqlite_file = "./owl/word.sqlite3"
# 默认独占(exclusive)模式使用sqlite会更快但不允许多程序并发访问, 可通过程序设定非独占
default_world.set_backend(filename = sqlite_file, exclusive=False)
default_world.save()
print('\t', f'save to {sqlite_file}')



print('2. Using several isolated Worlds')
my_world = World()
my_second_word = World(filename='owl/quadstore.sqlite3')
onto = my_world.get_ontology('http://test.org/onto/').load()
# print(my_world['http://test.org/onto/my_iri'])
sync_reasoner(my_world)
# URL: 是URI的子集, 统一资源定位符(Uniform Resource Locator)
# IRI: 国际化资源标识符（Internationalized Resource Identifiers，简称IRI）采用Unicode
# URI: 统一资源标识符（Uniform Resource Identifier)采用ASCII


print('3. working with RDFlib')
graph = default_world.as_rdflib_graph()
r = list(graph.query("""SELECT ?p WHERE {
  <http://www.semanticweb.org/jiba/ontologies/2017/0/test#ma_pizza> <http://www.semanticweb.org/jiba/ontologies/2017/0/test#price> ?p .
}"""))
print('\t', r)
r = list(graph.query_owlready("""SELECT ?p WHERE {
  <http://www.semanticweb.org/jiba/ontologies/2017/0/test#ma_pizza> <http://www.semanticweb.org/jiba/ontologies/2017/0/test#price> ?p .
}"""))
print('\t', r)
bn = graph.BNode()
with onto:
    graph.add((bn, rdflib.URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), rdflib.URIRef("http://www.w3.org/2002/07/owl#Class")))